package com.bkbw.live.battle.util;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.bkbw.live.battle.map.MapAStarInfo;
import com.bkbw.live.battle.map.MapManager;

import scala.Math;

public class AStarUtil {
	public static MapAStarInfo loadMapAStarInfoFromJsonFile(String path){
		MapAStarInfo info = null;
		//读取文件
		String str = FileUtils.getStringFromFile(path);
		System.out.println(str);
		//转为json
		JSONObject object = (JSONObject) JSONValue.parse(str);
		//读取地图的宽高，创建地图信息
		float unit = 1f;//Unity中的1个单位对应网格坐标系的多少个格子,暂时为1:1
		float width = Float.parseFloat((String) object.get("width"));
		float height = Float.parseFloat((String) object.get("height"));
		int rows = (int)Math.round(height/unit);
		int cols = (int)Math.round(width/unit);
		System.out.println(rows+"行"+cols+"列");
		info = new MapAStarInfo(cols,rows);
		JSONArray arr = (JSONArray) object.get("objects");
		for(int i=0;i<arr.size();i++){
			//根据每个碰撞体的边界，设置地图阻挡点,阻挡点是A＊坐标系
			JSONObject o = (JSONObject) arr.get(i);
			float x = Float.parseFloat((String) o.get("x"));
			float z = Float.parseFloat((String) o.get("z"));
			float w = Float.parseFloat((String) o.get("width"));
			float h = Float.parseFloat((String) o.get("height"));
			//左下角
			Point ld = new Point();
			ld.x = round((x-w/2)/unit);
			ld.y = round((z-h/2)/unit);
			//System.out.println("左下角"+ld.x+","+ld.y);
			//左上角
			Point lu = new Point();
			lu.x = round((x-w/2)/unit);
			lu.y = round((z+h/2)/unit);
			//System.out.println("左下角"+lu.x+","+lu.y);
			//右下角网格
			Point rd = new Point();
			rd.x = round((x+w/2)/unit);
			rd.y = round((z-h/2)/unit);
			//System.out.println("右下角"+rd.x+","+rd.y);
			//右上角网格
			Point ru = new Point();
			ru.x = round((x+w/2)/unit);
			ru.y = round((z+h/2)/unit);
			//System.out.println("右上角"+ru.x+","+ru.y);
			//对这4个网格点进行遍历，把之间的所有网格点全部设置为不可通过点
			for(int ii=ld.x;ii<rd.x;ii++){
				for(int jj=ld.y;jj<lu.y;jj++){
					info.setBarrierPoint(ii,jj,1);
					//System.out.println(ii+","+jj);
				}
			}
		}
		//组装AStarInfo
		return info;
	}
	
	public static void main(String[] args) {
		MapAStarInfo info = MapManager.getInstance().getMapInfoById("1");
//		List<Vector3D> list = info.findPath(new Vector3D(16,0,0), new Vector3D(16,0,20));
//		for(Vector3D v:list){
//			System.out.println(v);
//		}
		
		int[][] data = info.getData();
		for(int i = info.getMaxZ()-1;i>=0;i--){
			for(int j = 0;j<info.getMaxX();j++){
				System.out.print(data[j][i]);
			}
			System.out.println();
		}
		
		System.out.println(round(0)); 
	}
	
	//对小数进行4舍5入，和Math.round()不同之处在于0.5会判定为0，>0.5才向上取整
	public static int round(float val){
		int nagtive = val>0?1:-1;
		//分离整数和小数
		int in = (int)val;
		float fo = (val - in)*nagtive;
		int add = 0;
		if(Math.abs(fo)>0.5){
			add = 1;
		}else{
			add = 0;
		}
		int result = in+add*nagtive;
		return result;
	}
}
